(function () {
    'use strict';

    angular
        .module('SURF')
        .controller('HeadController', HeadController);

    HeadController.$inject = ['$scope', '$translate','CookieStoreService','$location','HeadService','COOKIE_STORE_KEYS','$route'];

    function HeadController($scope, $translate,CookieStoreService,$location,HeadService,COOKIE_STORE_KEYS,$route) {

        $('#oldPwd').password();

        $('#newPwdFirst').password();

        $('#newPwdSecond').password();


        $scope.switching = switchLanguage;
        function switchLanguage(lang) {
            $translate.use(lang);
            window.localStorage.lang = lang;
            $route.reload();
        }

        $scope.showModalDialog = showModalDialog;
        
        function showModalDialog() {
            $('#oldPwd').password('hide');
            $('#oldPwd').password().val("");
            $('#newPwdFirst').password('hide');
            $('#newPwdFirst').password().val("");
            $('#newPwdSecond').password('hide');
            $('#newPwdSecond').password().val("");

            $("#changePwdModal").modal("toggle");
            // showCommonDialog('333',1);
        }

        if(isNull($translate.use())){
            $scope.cur_lang = 'zh';
        }else {
            $scope.cur_lang = $translate.use();
        }


        $scope.loginOut = loginOut;

        function loginOut() {
            CookieStoreService.destroy();
            $location.path('/curve');
        }

        $scope.changePwd = changePwd;

        function changePwd() {
            var bean ={};
            bean.oldPwd = $('#oldPwd').password().val();
            bean.newPwdFirst = $('#newPwdFirst').password().val();
            bean.newPwdSecond = $('#newPwdSecond').password().val();



            if(isNull(bean.oldPwd)||isNull(bean.newPwdFirst)||isNull(bean.newPwdSecond)){
                showCommonDialog($translate.instant("PWD_NOT_NULL"),2);
                return;
            }

            if(bean.newPwdFirst!=bean.newPwdSecond){
                showCommonDialog($translate.instant("PWD_ERROR"),2);
                return;
            }

            if(bean.newPwdFirst.length>10||bean.newPwdFirst.length<6){
                showCommonDialog($translate.instant("A00_11"),2);
                return;
            }
            bean.cTaiId = CookieStoreService.getValue(COOKIE_STORE_KEYS.USER_ID);;
            HeadService.changePwd(bean).then(
                function (answer) {
                    var response = answer.data;
                    if (response.resultCode == 200) {
                        showCommonDialog($translate.instant("MODIFY_SUCCESS"),1);
                        $('#changePwdModal').modal('hide');
                        $scope.oldPwd="";
                        $scope.newPwdFirst="";
                        $scope.newPwdSecond="";
                    } else if(response.resultCode == 505) {
                        showCommonDialog($translate.instant("PASSWORD_ERROR"),2);
                    }
                }, function (error) {
                    $scope.error = error;
                })
        }


    }
})();
